/* NOTICE
    OpenJaWS - Open Java Weather Station
    
    Copyright (C) 2008 Grant Gardner <grant@lastweekend.com.au>
    
    OpenJaWS is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
NOTICE */
package au.com.lastweekend.openjaws.api;

import java.util.Date;

import au.com.lastweekend.openjaws.units.Unit;

/**
 * Structure to encapsulate a current, minimum and maximum value
 * 
 * After initialising max and min values further calls to set current that
 * supply a reading time will update max/min if necessary.
 * 
 * Will not return null values.
 * 
 * @author ggardner
 *
 */
public class CurrentMinMax {

    public CurrentMinMax() {

        //
    }

    public CurrentMinMax(CurrentMinMax obj) {

        super();
        this.current = obj.current;
        this.min = obj.min;
        this.max = obj.max;
    }

    private UnitValue current;
    private RecordedValue min;
    private RecordedValue max;

    public void setCurrent(double current, Unit unit) {

        this.setCurrent(current, unit, null);

    }

    public void setCurrent(double current, Unit unit, Date readingTime) {

        setCurrent(new UnitValue(current, unit), readingTime);

    }

    public void setMinimum(double minimum, Unit unit, Date timeMinimum) {

        this.min = new RecordedValue(timeMinimum, minimum, unit);
    }

    public void setMaximum(double maximum, Unit unit, Date timeMaximum) {

        this.max = new RecordedValue(timeMaximum, maximum, unit);
    }

    public UnitValue getCurrent() {

        return protectNull(current);
    }

    public RecordedValue getMin() {

        return protectNull(min);
    }

    public RecordedValue getMax() {

        return protectNull(max);
    }

    protected void setMinimum(UnitValue min, Date timeMin) {

        this.min = new RecordedValue(timeMin, min);
    }

    protected void setMaximum(UnitValue max, Date timeMax) {

        this.max = new RecordedValue(timeMax, max);
    }

    protected void setCurrent(UnitValue argCurrent, Date readingTime) {

        this.current = argCurrent;

        if (readingTime != null) {
            if (min != null && this.current.compareTo(min.getUnitValue()) < 0) {
                setMinimum(argCurrent, readingTime);
            }
            if (max != null && this.current.compareTo(max.getUnitValue()) > 0) {
                setMaximum(argCurrent, readingTime);
            }
        }
    }

    protected UnitValue protectNull(UnitValue uv) {

        if (uv == null) {
            return UnitValue.NULL;
        }
        return uv;
    }

    protected RecordedValue protectNull(RecordedValue rv) {

        if (rv == null) {
            return RecordedValue.NULL;
        } else {
            return rv;
        }
    }

    public String toString() {

        return String.format("Current = %s Max = %s, Min = %s", getCurrent(), getMax(), getMin());
    }

}